package com.zhai.netty.book.chapter10;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.CharsetUtil;

public class HttpServlethadnler102 extends SimpleChannelInboundHandler<FullHttpRequest>{
	
	private final String url;
	
	public HttpServlethadnler102(String url) {
		this.url = url;
	}
	
	/**
	 * 自己写测试demon
	 */
	@Override
	protected void messageReceived(ChannelHandlerContext ctx,FullHttpRequest msg) throws Exception {
		FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
		response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html;charset=UTF-8");
		StringBuffer buf = new StringBuffer();
		buf.append("<!DOCTYPE html>\r\n");
        buf.append("<html><head></head><title>远netty教程</title>\r\n");
        buf.append("<h1>你好啊二远</h1>\r\n</html>");
        ByteBuf buffer = Unpooled.copiedBuffer(buf,CharsetUtil.UTF_8);  
        response.content().writeBytes(buffer);  
        buffer.release();  
        ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); 
	}

}
